package com.ruoyi.project.propertySystem.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.propertySystem.domain.InspectFormwork;
import com.ruoyi.project.propertySystem.domain.InspectItem;
import com.ruoyi.project.propertySystem.domain.StationInspectFormwork;
import com.ruoyi.project.propertySystem.mapper.InspectFormworkMapper;
import com.ruoyi.project.propertySystem.mapper.InspectItemMapper;
import com.ruoyi.project.propertySystem.mapper.StationInspectFormworkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.propertySystem.mapper.FormworkMapper;
import com.ruoyi.project.propertySystem.domain.Formwork;
import com.ruoyi.project.propertySystem.service.IFormworkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 巡检模板Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-18
 */
@Service
public class FormworkServiceImpl implements IFormworkService 
{
    @Autowired
    private FormworkMapper formworkMapper;
    @Autowired
    private InspectItemMapper inspectItemMapper;
    @Autowired
    private InspectFormworkMapper inspectFormworkMapper;
    @Autowired
    private StationInspectFormworkMapper stationInspectFormworkMapper;

    /**
     * 查询巡检模板
     * 
     * @param id 巡检模板ID
     * @return 巡检模板
     */
    @Override
    public Formwork selectFormworkById(Long id)
    {
        return formworkMapper.selectFormworkById(id);
    }

    /**
     * 查询巡检模板列表
     * 
     * @param formwork 巡检模板
     * @return 巡检模板
     */
    @Override
    public List<Formwork> selectFormworkList(Formwork formwork)
    {
        return formworkMapper.selectFormworkList(formwork);
    }

    /**
     * 新增巡检模板
     * 
     * @param formwork 巡检模板
     * @return 结果
     */
    @Override
    public int insertFormwork(Formwork formwork)
    {

        try {
            if (formwork.getItems().length == 0) {
                throw new BaseException("请选择巡检项");
            }
            formwork.setGmtCreate(new Date());
            formwork.setCreateBy(SecurityUtils.getUsername());
            int i = formworkMapper.insertFormwork(formwork);
            if (i > 0) {
                List<InspectFormwork> inspectFormworks = new ArrayList<>();
                Arrays.stream(formwork.getItems()).forEach(x -> {
                    InspectFormwork inspectFormwork = new InspectFormwork();
                    inspectFormwork.setFormworkId(formwork.getId());
                    InspectItem inspectItem = inspectItemMapper.selectInspectItemByItemSn(x);
                    inspectFormwork.setItemId(inspectItem.getId());
//                    InspectItem inspectItem = inspectItemMapper.selectInspectItemById(x);
                    inspectFormwork.setClassifyId(inspectItem.getClassifyId());
                    inspectFormwork.setCreateBy(SecurityUtils.getUsername());
                    inspectFormwork.setGmtCreate(new Date());
                    inspectFormworks.add(inspectFormwork);
                });
                if (inspectFormworks.size() > 0) {
                    inspectFormworkMapper.batchInsert(inspectFormworks);
                }
            }
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("新建模板保存失败");
        }
    }

    /**
     * 修改巡检模板
     * 
     * @param formwork 巡检模板
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFormwork(Formwork formwork)
    {
        try {
            if (formwork.getItems().length == 0) {
                throw new BaseException("请选择巡检项");
            }
            //删除之前对应的巡检项
            int i = inspectFormworkMapper.deleteInspectFormworkByFormworkId(formwork.getId());
            List<InspectFormwork> inspectFormworks = new ArrayList<>();
            Arrays.stream(formwork.getItems()).forEach(x -> {
                InspectFormwork inspectFormwork = new InspectFormwork();
                inspectFormwork.setFormworkId(formwork.getId());
                InspectItem inspectItem = inspectItemMapper.selectInspectItemByItemSn(x);
                inspectFormwork.setItemId(inspectItem.getId());
//                InspectItem inspectItem = inspectItemMapper.selectInspectItemById(x);
                inspectFormwork.setClassifyId(inspectItem.getClassifyId());
                inspectFormwork.setCreateBy(SecurityUtils.getUsername());
                inspectFormwork.setGmtCreate(new Date());
                inspectFormworks.add(inspectFormwork);
            });
            if (inspectFormworks.size() > 0) {
                inspectFormworkMapper.batchInsert(inspectFormworks);
            }
            formwork.setGmtModify(new Date());
            formwork.setCreateBy(SecurityUtils.getUsername());
            return formworkMapper.updateFormwork(formwork);
        } catch (BaseException e) {
            e.printStackTrace();
            throw new BaseException("编辑巡检模板失败");
        }
    }

    /**
     * 批量删除巡检模板
     * 
     * @param ids 需要删除的巡检模板ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFormworkByIds(Long[] ids)
    {
        Arrays.stream(ids).forEach(x->{
            StationInspectFormwork stationInspectFormwork = new StationInspectFormwork();
            stationInspectFormwork.setFormworkId(x);
            List<StationInspectFormwork> stationInspectFormworks = stationInspectFormworkMapper.selectStationInspectFormworkList(stationInspectFormwork);
            if(stationInspectFormworks.size()>0){
                throw new BaseException("模板目前正在使用请勿删除,请联系管理员");
            }
        });
        return formworkMapper.deleteFormworkByIds(ids);
    }

    /**
     * 删除巡检模板信息
     * 
     * @param id 巡检模板ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFormworkById(Long id)
    {
        StationInspectFormwork stationInspectFormwork = new StationInspectFormwork();
        stationInspectFormwork.setFormworkId(id);
        List<StationInspectFormwork> stationInspectFormworks = stationInspectFormworkMapper.selectStationInspectFormworkList(stationInspectFormwork);
        if(stationInspectFormworks.size()>0){
            throw new BaseException("模板目前正在使用请勿删除,请联系管理员");
        }
        return formworkMapper.deleteFormworkById(id);
    }
}
